﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufTree.h"
#include "GnufQtItem.h"
#include <QTreeWidgetItem>
#include <QEvent>

namespace gnuf {
    class Info;
    class QtTreeItemDelegate;
    class TreeViewAppearanceSetting;
    // this is not a QObject
    // it can not inherit from QtItemWith

    class QtTreeItem : public QTreeWidgetItem, public QtItem
    {
        friend class QtTreeItemDelegate;
    public:
        explicit QtTreeItem(TreeNode*, QTreeWidget* parent);
        explicit QtTreeItem(TreeNode*, QTreeWidgetItem* parent);
        virtual ~QtTreeItem();
        virtual void refresh(QtItem::EWhatDirty) override;
        TreeNode * item() const { return m_item; }

    public:
        void setStyle();
        void setActive(bool bActive) { m_bIsActive = bActive; }
        inline bool isActived() const { return m_bIsActive; }
        void addChildNode(QtTreeItem* childNode);
        void insertChildBefore(QtTreeItem* treeItem, QtTreeItem* childNode);
        void insertChildAfter(QtTreeItem* treeItem, QtTreeItem* childNode);

    public:
        void setNodeInfo(int col, const TreeNode::NodeInfo* nodeInfo);
        void setTextNode(int col, const TreeNode::NodeInfo* nodeInfo);
        void setTextEditorNode(int col, const TreeNode::NodeInfo* nodeInfo);
        void setButtonNode(int col, const TreeNode::NodeInfo* nodeInfo);
        void setCheckBoxNode(int col, const TreeNode::NodeInfo* nodeInfo);
        void setListNode(int col, const TreeNode::NodeInfo* nodeInfo);
        void setNodeIcon(int col, const TreeNode::NodeInfo* nodeInfo);

        void setPressColumn(int col) {
            m_pressCol = col;
        }
        int pressColumn() const {
            return m_pressCol;
        }

        void enabledDrag(bool);
        bool isEnabledDrag() const;

        void enableEdit(bool);
        bool isEnabledEdit() const;

        void setSelectable(bool);

        void deleteLater();
        bool operator<(const QTreeWidgetItem&) const;

    public:
        virtual void destroy() override
        {
            destroyImmediately();
        }

        // Disconnect from the item.
        virtual void disconnectFromItem() override final
        {
            if (nullptr != m_item)
            {
                m_item->setQtItem(nullptr);
                m_item = nullptr;
            }
        }

        virtual QWidget * castQWidget() override { return nullptr; }
        virtual const QWidget * castQWidget() const override { return nullptr; }
    private:
        void setRowDelegate(QtTreeItem*);
        const TreeViewAppearanceSetting* appearanceSetting();
        QString iconPath(const Info* info, EIconStatus status = EIconStatus::eDefault);
        void setColumnCount(int count);

    private:
        bool m_bIsActive;
        TreeNode * m_item;
        int m_pressCol;
    };
}
